#!/bin/sh

# 启动多个服务  ./startBlogAll.sh start
# 关闭多个服务  ./startBlogAll.sh stop
# 重启多个服务  ./startBlogAll.sh restart
pp=$(pwd)
echo $pp
export BLOG_WEB=$pp/blog-web.jar
export BLOG_ADMIN=$pp/blog-admin.jar

export BLOG_WEB_port=8443
export BLOG_ADMIN_port=8085


case "$1" in

start)
      ## 启动BLOG_WEB
      echo "BLOG_WEB开始启动----------------------"
      nohup /usr/bin/java -jar $BLOG_WEB > $pp/log-blog-web.log 2>&1 &
      BLOG_WEB_pid=`lsof -i:$BLOG_WEB_port|grep "LISTEN"|awk '{print $2}'`
      until [ -n "$BLOG_WEB_pid" ]
          do
            BLOG_WEB_pid=`lsof -i:$BLOG_WEB_port|grep "LISTEN"|awk '{print $2}'`
          done
      echo "----------------------BLOG_WEB启动成功 pid:$BLOG_WEB_pid"

      ## 启动admin
      echo "BLOG_ADMIN开始启动--------------------------"
      nohup /usr/bin/java -jar $BLOG_ADMIN > $pp/log-blog-admin.log 2>&1 &
      BLOG_ADMIN_pid=`lsof -i:$BLOG_ADMIN_port|grep "LISTEN"|awk '{print $2}'`
      until [ -n "$BLOG_ADMIN_pid" ]
          do
            BLOG_ADMIN_pid=`lsof -i:$BLOG_ADMIN_port|grep "LISTEN"|awk '{print $2}'`
          done
      echo "--------------------------BLOG_ADMIN启动成功 pid:$BLOG_ADMIN_pid"
      echo "===startAll success==="
      ;;

 stop)
      P_ID=`ps -ef | grep -w $BLOG_WEB | grep -v "grep" | awk '{print $2}'`
      if [ "$P_ID" == "" ]; then
          echo "===BLOG_WEB process not exists or stop success"
      else
          kill -9 $P_ID
          echo "BLOG_WEB killed success"
      fi

      P_ID=`ps -ef | grep -w $BLOG_ADMIN | grep -v "grep" | awk '{print $2}'`
      if [ "$P_ID" == "" ]; then
          echo "===BLOG_ADMIN process not exists or stop success"
      else
          kill -9 $P_ID
          echo "BLOG_ADMIN killed success"
      fi

      echo "===stop success==="
      ;;

restart)
        $0 stop
        sleep 2
        $0 start
        echo "===restart success==="
        ;;
esac
exit 0